#!/bin/bash

###
# mvcsetup - Bash script to help checkout an ASG_MVC project from Subversion.
############################################################################################

###
# Customize on a per-server basis:
#
# ASGMVC_APP_TMP      -	The directory where the application will place temporary Smarty 
#                       php files as well as PEAR::Log files if file-based logging is 
#                       enabled in Config.php
#
# APP_DIR_GROUP_OWNER - The system group that group ownership of the app's directories will 
#                       be assigned
#
# TMP_DIR_GROUP_OWNER - The system group that group ownership of the app's tmp directories 
#                       will be assigned
#
# LOCAL_TMP_DIR       - The name of the directory in the app's root dir where Smarty and Log
#                       tmp directories will be created if the -lt switch is passed
#
############################################################################################
ASGMVC_APP_TMP=/var/www/asgmvc_app_tmp
APP_DIR_GROUP_OWNER=asgwww
TMP_DIR_GROUP_OWNER=asgwww
LOCAL_TMP_DIR=_private




###
# No customization should be necessary beyond this point.
############################################################################################

function help {
	echo ""
	echo "mvcsetup - A utility to help checkout an ASG_MVC project from Subversion."
	echo ""
	echo "Usage: mvcsetup [options]"
	echo ""
	echo "Options:"
	echo "	-r repository		The URL or PATH to the Subversion repository."
	echo "				REQUIRED."
	echo ""
	echo "	-w work_dir		The name to checkout the project as."
	echo "				REQUIRED"
	echo ""
	echo "	-p work_dir_path	The path in which to checkout the project."
	echo "				OPTIONAL: Defaults to './'"
	echo ""
	echo "	-t temp_dir		The name to of the temporary files dir to use."
	echo "				OPTIONAL: Defaults to 'username.work_dir'"
	echo "				NOTE:     This directory will be created in"
	echo "				          $ASGMVC_APP_TMP"
	echo ""
	echo "	-lt			OPTIONAL: Create a local tmp dir '$LOCAL_TMP_DIR' in the"
	echo "				          app's root directory instead of in"
	echo "                                    $ASGMVC_APP_TMP"
	echo ""
	echo "	-e			Do an export, rather than a checkout operation."
	echo ""
	echo ""
	echo "Examples:"
	echo "	% mvcsetup -r svn://192.168.1.25/myproject/main -w myproject"
	echo ""
	echo "	% mvcsetup -r svn://192.168.1.25/myproject/main -w myproject -p /some/path -t somedir"
	echo ""
	echo ""
}


do_export=0
repos="null"
wdir="null"
wdpath=.
tmpdir=$USER
localtmpdir=0

while [ $# -gt 0 ]
do
    case "$1" in
	-r)	repos="$2"; shift;;
	-w)	wdir="$2"; shift;;
	-p)	wdpath="$2"; shift;;
	-t)	tmpdir="$2"; shift;;
	-lt)	localtmpdir=1; shift;;
	-e) 	do_export=1; shift;;
	-h)
		help
		exit 1;;
	*)	break;;		# terminate while loop
    esac
    shift
done

if [ $repos = "null" ]; then
	help
	exit 1
fi

if [ $wdir = "null" ]; then
	help
	exit 1
fi

if [ $tmpdir = $USER ]; then
	tmpdir=$USER.$wdir
fi

if [ $localtmpdir = 1 ]; then
	tmpdir=$LOCAL_TMP_DIR
else
	tmpdir=$ASGMVC_APP_TMP/$tmpdir
fi


# svn export
if [ $do_export = 1 ]; then
	echo "Exporting project from subversion..."
	svn export $repos $wdpath/$wdir
# svn checkout
else
	echo "Checking out project from subversion..."
	svn co $repos $wdpath/$wdir
fi

# create symlink for includes dir
cd $wdpath/$wdir
echo "" 
echo "Creating includes dir symlink..."
ln -s Presentation/Smarty/Default/includes

# create a myConfig.php
echo "Copying myConfig.php..."
cp myConfig_template.php myConfig.php

# create .htaccess
echo "Copying .htaccess..."
cp DOT_htaccess .htaccess

# change group permissions
chgrp -R $APP_DIR_GROUP_OWNER *

# setup tmp dirs
echo "Creating tmp dirs..."
mkdir $tmpdir
mkdir $tmpdir/logs
mkdir $tmpdir/smarty/
mkdir $tmpdir/smarty/templates_c
chgrp -R $TMP_DIR_GROUP_OWNER $tmpdir
chmod -R g+w $tmpdir

echo "" 
echo "Finished. Don't forget to edit your .htaccess and myConfig.php files."
echo "" 
